<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Sys_app extends CI_Controller
{
	public $userInfo;
	public function _load()
	{
		$this->load->model('User_model');
		$this->load->library('user_verification');
		$this->load->model('Number_model');
		$this->load->model('My_address_model');
	}
	function __construct()
	{
		parent::__construct();
		$this->_load();
		$is_login = $this->user_verification->is_login();
		if($is_login == 0)
		{
			redirect('sign/login', 'location', 303);
		}
		$userInfo = $this->User_model->getForId($this->input->cookie('user_id', TRUE));
		$this->userInfo = $userInfo[0];
		if($userInfo[0]['is_admin'] == 0)
		{
			redirect('member/dashboard', 'location', 303);
		}
	}
	function index()
	{
		
	}
	function addPostNumber()
	{
		$data['userInfo'] = $this->userInfo;
		$data['post_number'] = $this->Number_model->getNumberNum();
		$this->load->view('member/public/head',$data);
		$this->load->view('member/public/left');
		$this->load->view('member/sys_app/addPostNumber');
		$this->load->view('member/public/foot');
	}
	function doAddNumber()
	{
		$data['userInfo'] = $this->userInfo;
		if($_FILES['userfile']['name'] == '')
		{
			$data['pay_result'] = array(
								'tips_img'=>'img/alert.gif',
								'tips_text'=>'<span style="color: rgb(241, 132, 31);">错误提示：非法操作！！！.</span><span>请重新选择表格上传，请核对信息。</span>',
				);
			$this->load->view('member/public/head',$data);
			$this->load->view('member/public/left');
			$this->load->view('member/public/pay_result');
			$this->load->view('member/public/foot');
		}
		else
		{
			$file_mime = strstr($_FILES['userfile']['name'], '.');
			if($file_mime == '.xls' || $file_mime == '.xlsx')
			{
				$tradecode = $this->common_func->createTradecode($_POST['tradecode']);
				if($tradecode != $data['userInfo']['tradecode'])
				{/*安全码出错*/
					$data['pay_result'] = array(
											'tips_img'=>'img/error.gif',
											'tips_text'=>'<span style="color: rgb(241, 132, 31);">订单导入失败：账户交易安全码输入错误</span><span>请<a href="'.site_url('member/order_center/add').'">返回</a>重新输入交易安全码，以保证账户安全。账户交易安全码忘记？<a hreff="'.site_url('member/member_center/profile').'">重新设置安全码>></a></span>',
					);
					$this->load->view('member/public/head',$data);
					$this->load->view('member/public/left');
					$this->load->view('member/public/pay_result');
					$this->load->view('member/public/foot');
				}
				else
				{
					$path = './upload/excel/load_number'.$file_mime;
					move_uploaded_file($_FILES['userfile']['tmp_name'], $path);
					$filePath = 'upload/excel/load_number'.$file_mime;
					$excelData = $this->common_func->readerExcel($filePath);
					$data['pay_result'] = $this->Number_model->addNumber($excelData);
					$this->load->view('member/public/head',$data);
					$this->load->view('member/public/left');
					$this->load->view('member/public/pay_result');
					$this->load->view('member/public/foot');
				}
			}
			else 
			{
				$data['pay_result'] = array(
								'tips_img'=>'img/alert.gif',
								'tips_text'=>'<span style="color: rgb(241, 132, 31);">系统错误：文件有误！！！.</span><span>选择正确的文件格式上传。</span>',
				);
				$this->load->view('member/public/head',$data);
				$this->load->view('member/public/left');
				$this->load->view('member/public/pay_result');
				$this->load->view('member/public/foot');
			}
		}
	}
	function downloadOrderData()
	{
		$data['userInfo'] = $this->userInfo;
		if(isset($_GET['page']))
		{
			$data['table_hover'] = 0;
			$downloadData = $this->Number_model->getForDown();
			if(count($downloadData) > 0)
			{
				$file_name = $this->common_func->writeExcel($downloadData);//生成表格
				if(count( $this->common_func->readerExcel($file_name) ) < 1)
				{
					unlink($file_name);
					exit('创建数据失败，请重试！！');
				}
				else 
				{
					$this->Number_model->setOk($downloadData);
				}
			}
			$this->load->helper('file');
			$data['excelData'] = get_dir_file_info('./download/');
			foreach ($data['excelData'] as $k=>$v)
			{
				$data['excelData'][$k]['num'] = count($this->common_func->readerExcel('./download/'.$v['name']));
			}
		}
		else 
		{
			$data['table_hover'] = 1;
			$data['order_data'] = $this->Number_model->getOrderDown();
		}
		$this->load->view('member/public/head',$data);
		$this->load->view('member/public/left');
		$this->load->view('member/sys_app/downloadOrder');
		$this->load->view('member/public/foot');
	}
	function doDownload()
	{
		if(isset($_GET['file']))
		{
			$this->load->helper('download');
			$data = file_get_contents('./download/'.$_GET['file']); // 读文件内容
			$name = $_GET['file'];
			force_download($name, $data);
		}
		else 
		{
			exit('出错！！！');
		}
	}
	function deleteFile()
	{
		if(isset($_GET['file']))
		{
			unlink('./download/'.$_GET['file']);
			redirect(site_url('member/sys_app/downloadOrderData?page=download'),'',303);
		}
		else 
		{
			exit('出错！！！');
		}
	}
	function siteConfig()
	{
		echo 1;
	}
	function appConfig()
	{
		echo 1;
	}
}